// Copyright (c) 2009, Michael Patraw
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * The name of Michael Patraw may not be used to endorse or promote 
//       products derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY Michael Patraw ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL Michael Patraw BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef UTIL_H
#define UTIL_H

#include <cassert>

enum Dir
{
    DIR_UP,
    DIR_DOWN,
    DIR_LEFT,
    DIR_RIGHT,
    DIR_UPLEFT,
    DIR_UPRIGHT,
    DIR_DOWNLEFT,
    DIR_DOWNRIGHT,
};

inline void GetDirection(Dir d, int * x, int * y)
{
    assert(x != NULL && y != NULL);
    *x = 0;
    *y = 0;
    switch (d) {
        case DIR_UP:
            --(*y);
            break;
        case DIR_DOWN:
            ++(*y);
            break;
        case DIR_LEFT:
            --(*x);
            break;
        case DIR_RIGHT:
            ++(*x);
            break;
        default:
            break;
    }
}

class Coord
{
public:
    Coord(int x=0, int y=0) :
    x_(x), y_(y)
    {
    }

    int getX() const { return x_; }
    int getY() const { return y_; }
    
    void setX(int x) { x_ = x; }
    void setY(int y) { y_ = y; }
    
    void modX(int dx) { x_ += dx; }
    void modY(int dy) { y_ += dy; }
private:
    int x_, y_;
};

class Rect
{
public:
    Rect(int x=0, int y=0, int w=0, int h=0) :
    x_(x), y_(y), w_(w), h_(h)
    {
    }
    
    bool isWithin(int x, int y) const { return (x >= 0 && x < w_ && y >= 0 && y < h_); }
    
    int getX() const { return x_; }
    int getY() const { return y_; }
    int getW() const { return w_; }
    int getH() const { return h_; }
    
    int getTop()   const { return y_; }
    int getBot()   const { return y_ + h_; }
    int getLeft()  const { return x_; }
    int getRight() const { return x_ + w_; }
    
    Coord getTopLeft()  const { return Coord(x_, y_); }
    Coord getBotLeft()  const { return Coord(x_, y_ + h_); }
    Coord getTopRight() const { return Coord(x_ + w_, y_); }
    Coord getBotRight() const { return Coord(x_ + w_, y_ + h_); }
    
    void setX(int x) { x_ = x; }
    void setY(int y) { y_ = y; }
    void setW(int w) { w_ = w; }
    void setH(int h) { h_ = h; }
private:
    int x_, y_, w_, h_;
};

#endif // UTIL_H
